<!DOCTYPE html>


<html lang="zh_CN">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>Nginx基本概念 |  MyBlog</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="MyBlog" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Nginx笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Nginx基本概念
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/12/04/Nginx%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-12-04T05:40:50.997Z" itemprop="datePublished">2021-12-04</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">7 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="Nginx基本概念"><a href="#Nginx基本概念" class="headerlink" title="Nginx基本概念"></a>Nginx基本概念</h1><h2 id="（1）nginx是什么"><a href="#（1）nginx是什么" class="headerlink" title="（1）nginx是什么"></a>（1）nginx是什么</h2><p><em>Nginx</em> (engine x) 是一个高性能的<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/HTTP">HTTP</a>和<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86/7793488">反向代理</a>web服务器，特点是占有内存少，并发能力强，事实上nginx的并发能力确实在同类型的网页服务器种表现较好</p>
<p>Nginx专为性能优化而开发，性能是其最重要的考量，实现上非常注重效率，能经受高负载的考验，有报告表明能支持高达50，000个并发连接数</p>
<h2 id="（2）反向代理"><a href="#（2）反向代理" class="headerlink" title="（2）反向代理"></a>（2）反向代理</h2><p>正向代理</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111301536557.png" alt="image-20211130153606036"></p>
<p>反向代理</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111301928260.png" alt="image-20211130192833776"></p>
<h2 id="（3）负载均衡"><a href="#（3）负载均衡" class="headerlink" title="（3）负载均衡"></a>（3）负载均衡</h2><p>单个服务器解决不了，我们增加服务器的数量，然后将请求分发到各个服务器上，将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上，将负载分发到不同的服务器，也就是我们所说的负载均衡</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111302003660.png" alt="image-20211130200303239"></p>
<h2 id="（4）动静分离"><a href="#（4）动静分离" class="headerlink" title="（4）动静分离"></a>（4）动静分离</h2><p>为了加快网站的解析速度，可以吧动态页面和静态页面由不同的服务器来解析，加快解析速度，降低原来单个服务器的压力</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111302009566.png" alt="image-20211130200932849"></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111302012594.png" alt="image-20211130201232918"></p>
<h2 id="安装nginx"><a href="#安装nginx" class="headerlink" title="安装nginx"></a>安装nginx</h2><p>(1) 安装 <strong>pcre依赖</strong></p>
<p>cd /usr/src</p>
<p><strong>第一步 联网下载</strong> <strong>pcre</strong> <strong>压缩文件依赖</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz</span><br></pre></td></tr></table></figure>

<p><strong>第二步 解压压缩文件</strong></p>
<p><strong>使用命令</strong> <strong>tar -xvf pcre-8.37.tar.gz</strong></p>
<p><strong>第三步</strong>./configure <strong>完成后，回到</strong> <strong>pcre</strong> <strong>目录下执行</strong> <strong>make</strong>，最后执行<strong>make install</strong></p>
<p><font color='red'>注意：使用命令的前提是有c++的环境支持 </font></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y gcc gcc-c++</span><br></pre></td></tr></table></figure>

<p><strong>（</strong>2）安装openssl 、zlib <strong>、</strong> <strong>gcc</strong> <strong>依赖</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel</span><br></pre></td></tr></table></figure>

<p><strong>（</strong>3**）安装 **nginx</p>
<p>安装位置 cd /usr/src下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget http://nginx.org/download/nginx-1.6.2.tar.gz</span><br></pre></td></tr></table></figure>

<p><strong>使用命令解压</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar zxvf nginx-1.6.2.tar.gz</span><br></pre></td></tr></table></figure>

<p><strong>./configure</strong></p>
<p><font color='red'>若出现以下错误：</font></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202201151542140.png" alt="image-20220115154158052"></p>
<p>则使用以下命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y zlib-devel</span><br></pre></td></tr></table></figure>

<p><strong>make &amp;&amp; make install</strong></p>
<p><strong>进入目录</strong> <strong>/usr/local/nginx/sbin/nginx</strong> <strong>启动服务</strong></p>
<p>在windows系统中访问linux中nginx，默认不能访问的，因为防火墙</p>
<p>查看开放的端口号：</p>
<p>firewall-cmd –list-all</p>
<p>设置开放的端口号：</p>
<p>firewall-cmd –add-service=http -permanent</p>
<p>sudo firewall-cmd –add-port=80/tcp –permanent</p>
<h1 id="nginx的常用命令和配置文件"><a href="#nginx的常用命令和配置文件" class="headerlink" title="nginx的常用命令和配置文件"></a>nginx的常用命令和配置文件</h1><h2 id="1-nginx操作的常用命令"><a href="#1-nginx操作的常用命令" class="headerlink" title="(1) nginx操作的常用命令"></a>(1) nginx操作的常用命令</h2><ol>
<li>使用nginx操作命令前提条件：必须进入nginx的目录 /usr/local/nginx/sbin</li>
<li>查看nginx版本号   ./nginx -v</li>
<li>启动nginx   ./nginx </li>
<li>关闭nginx  ./nginx -s stop</li>
<li>重新加载nginx(不需重启服务器，配置文件也能生效)   ./nginx -s reload</li>
</ol>
<h2 id="2-nginx的配置文件"><a href="#2-nginx的配置文件" class="headerlink" title="(2) nginx的配置文件"></a>(2) nginx的配置文件</h2><p>位置：/usr/local/nginx/conf/nginx.conf</p>
<p>配置文件的组成</p>
<p>（1）nginx配置文件有三部分组成</p>
<p>第一部分  全局块</p>
<p>主要设置会影响nginx服务器整体运行的配置指令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">worker_processes  1</span><br></pre></td></tr></table></figure>

<p>这是Nginx服务器并发处理服务的关键配置，worker_processes值越大，可以支持的并发处理也越多,但是会会受到硬件、软件等设备的制约</p>
<p>第二部分  events块</p>
<p>主要影响Nginx服务器与用户的网络连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">worker_connections 1024  <span class="comment">#表示支持最大的连接数</span></span><br></pre></td></tr></table></figure>

<p>第三部分 http块</p>
<p>Nginx 服务器配置中最频繁的部分</p>
<p>http块也可以包括http全局块、Server块</p>
<h1 id="Nginx配置实例-反向代理"><a href="#Nginx配置实例-反向代理" class="headerlink" title="Nginx配置实例 - 反向代理"></a>Nginx配置实例 - 反向代理</h1><h2 id="1-实现效果"><a href="#1-实现效果" class="headerlink" title="1.实现效果"></a>1.实现效果</h2><p>（1）打开浏览器，在浏览器地址栏中输入地址<a target="_blank" rel="noopener" href="http://www.123.com,跳转linux系统tomcat主页面/">www.123.com，跳转linux系统tomcat主页面</a></p>
<h2 id="2-准备工作"><a href="#2-准备工作" class="headerlink" title="2.准备工作"></a>2.准备工作</h2><p>在linux系统安装tomcat，使用默认端口8080</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111302324488.png" alt="image-20211130232400021"></p>
<h2 id="3-具体配置"><a href="#3-具体配置" class="headerlink" title="3.具体配置"></a>3.具体配置</h2><p>第一步，在windows系统的host文件进行域名和ip对应关系</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202201151511512.png" alt="image-20211130233503185"></p>
<p>配置内容：</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112052253988.png" alt="image-20211205225310605"></p>
<p>第二步，在nginx进行请求转发的配置（反向代理配置）</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202111302347050.png" alt="image-20211130234703832"></p>
<p>第三步，重载nginx      ./nginx  -s  reload</p>
<p>location指令说明</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011644722.png" alt="image-20211201002302685"></p>
<h1 id="负载均衡"><a href="#负载均衡" class="headerlink" title="负载均衡"></a>负载均衡</h1><h2 id="1-实现效果-1"><a href="#1-实现效果-1" class="headerlink" title="1.实现效果"></a>1.实现效果</h2><p>（1）浏览器地址栏输入地址 <a target="_blank" rel="noopener" href="http://192.168.227.20/edu/a.html,%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%95%88%E6%9E%9C%EF%BC%8C%E5%B9%B3%E5%9D%878080%E5%92%8C8081%E7%AB%AF%E5%8F%A3%E4%B8%AD">http://192.168.227.20/edu/a.html,负载均衡效果，平均8080和8081端口中</a></p>
<h2 id="2-准备工作-1"><a href="#2-准备工作-1" class="headerlink" title="2.准备工作"></a>2.准备工作</h2><p>（1）准备两台tomcat服务器，一台8080，一台8082</p>
<p>（2）在两台tomcat里面webapps目录中，创建名称是edu文件夹，在edu文件夹中创建页面a.html,用于测试</p>
<h2 id="3-在nginx的配置文件中进行负载均衡的配置"><a href="#3-在nginx的配置文件中进行负载均衡的配置" class="headerlink" title="3.在nginx的配置文件中进行负载均衡的配置"></a>3.在nginx的配置文件中进行负载均衡的配置</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112012331053.png" alt="image-20211201233127855"></p>
<h2 id="4-nginx分配服务器策略"><a href="#4-nginx分配服务器策略" class="headerlink" title="4.nginx分配服务器策略"></a>4.nginx分配服务器策略</h2><p><strong>第一种 轮询（默认）</strong></p>
<p><strong>每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器</strong> <strong>down</strong> <strong>掉，能自动剔除。</strong></p>
<p><strong>第二种</strong> <strong>weight</strong></p>
<p><strong>weight</strong> <strong>代表权重默认为</strong> <strong>1,权重越高被分配的客户端越多</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011653336.png" alt="image-20211201165325741"></p>
<p><strong>第三种</strong> <strong>ip_hash</strong></p>
<p><strong>每个请求按访问</strong> <strong>ip</strong> <strong>的</strong> <strong>hash</strong> <strong>结果分配，这样每个访客固定访问一个后端服务器，解决session共享的问题</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011918766.png" alt="image-20211201191851580"></p>
<p><strong>第四种</strong> <strong>fair</strong>（第三方,需要自己下载nginx-upstream-fair-master）</p>
<p><strong>按后端服务器的响应时间来分配请求，响应时间短的优先分配。</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011919235.png" alt="image-20211201191946917"></p>
<h1 id="动静分离"><a href="#动静分离" class="headerlink" title="动静分离"></a>动静分离</h1><p>把动态跟静态请求分开</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011931394.png" alt="image-20211201193105808"></p>
<blockquote>
<p>动静分离目前从实现角度来讲大致分为两种。</p>
</blockquote>
<p>一种，是纯粹把静态文件独立成单独的域名，放在独立的服务器上，也是目前主流推崇的方案</p>
<p>另一种是动态跟静态文件混合在一起发布，通过nginx来分开</p>
<blockquote>
<p>准备工作</p>
</blockquote>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112011947053.png" alt="image-20211201194750893"></p>
<blockquote>
<p>具体配置</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">location /www/ &#123;</span><br><span class="line">    root /data/;</span><br><span class="line">    index index.html index.htm;</span><br><span class="line">&#125;</span><br><span class="line">location /image/ &#123;</span><br><span class="line">    root /data/;</span><br><span class="line">    autoindex on;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>最终测试</p>
</blockquote>
<p><a target="_blank" rel="noopener" href="http://192.168.227.20/image/2.jpg">http://192.168.227.20/image/2.jpg</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">autoindex on   <span class="comment">#列出当前文件夹的内容</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112012001766.png" alt="image-20211201200127602"></p>
<h1 id="Nginx配置高可用的集群"><a href="#Nginx配置高可用的集群" class="headerlink" title="Nginx配置高可用的集群"></a>Nginx配置高可用的集群</h1><h2 id="1-什么是nginx高可用"><a href="#1-什么是nginx高可用" class="headerlink" title="1.什么是nginx高可用"></a>1.什么是nginx高可用</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112012018835.png" alt="image-20211201201837753"></p>
<p>（1）需要两台nginx服务器</p>
<p>（2）需要keepalived</p>
<p>（3）需要虚拟ip</p>
<h2 id="2-准备工作-2"><a href="#2-准备工作-2" class="headerlink" title="2.准备工作"></a>2.准备工作</h2><p>（1）需要两台服务器</p>
<p>（2）在两台服务器上安装nginx</p>
<p>（3）在两台服务器安装keepalived</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install keepalived</span><br></pre></td></tr></table></figure>

<h2 id="3-完成高可用配置（主从配置）"><a href="#3-完成高可用配置（主从配置）" class="headerlink" title="3.完成高可用配置（主从配置）"></a>3.完成高可用配置（主从配置）</h2><p>（1）修改/etc/keepalived/keepalived.conf配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#全局配置</span></span><br><span class="line">global_defs &#123;</span><br><span class="line">   notification_email &#123;</span><br><span class="line">     acassen@firewall.loc</span><br><span class="line">     failover@firewall.loc</span><br><span class="line">     sysadmin@firewall.loc</span><br><span class="line">   &#125;</span><br><span class="line">   notification_email_from Alexandre.Cassen@firewall.loc</span><br><span class="line">   smtp_server 192.168.227.30</span><br><span class="line">   smtp_connect_timeout 30</span><br><span class="line">   router_id LVS_DEVEL   <span class="comment">#通过它可以访问到主机，在/etc/hosts配置路由id</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">#脚本配置</span></span><br><span class="line">vrrp_script chk_http_port &#123;</span><br><span class="line">	script <span class="string">&quot;/usr/local/src/nginx_check.sh&quot;</span></span><br><span class="line">	interval 2  <span class="comment">#检测脚本执行的间隔</span></span><br><span class="line">	weight 2    <span class="comment">#设置当前服务器的权重</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">#虚拟IP配置</span></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state BACKUP  <span class="comment">#表示该服务器是主服务器(MASTER)还是备份服务器(BACKUP)</span></span><br><span class="line">    interface ens33 <span class="comment">#网卡接口，通过ifconfig可以查询</span></span><br><span class="line">    virtual_router_id 51  <span class="comment">#主、备机的id值，必须相同</span></span><br><span class="line">    priority 90 <span class="comment">#表示优先级，值越大优先级越高</span></span><br><span class="line">    advert_int 1</span><br><span class="line">    authentication &#123;  <span class="comment">#校验权限方式</span></span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123; </span><br><span class="line">        192.168.227.50 <span class="comment"># VRRP H虚拟地址</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主机只需要修改网卡名还有优先级</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">interface eno16777736</span><br><span class="line">priority 100</span><br></pre></td></tr></table></figure>

<p>主机的名字(LVS_DEVEL) /etc/hosts下，需要自己配置</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112012219765.png"></p>
<p>（2）在/usr/local/src添加检测脚本nginx_check.sh</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line">A=`ps -C nginx –no-header |wc -l`</span><br><span class="line">if [ $A -eq 0 ];then</span><br><span class="line">    /usr/local/nginx/sbin/nginx</span><br><span class="line">    sleep 2</span><br><span class="line">    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then</span><br><span class="line">        killall keepalived</span><br><span class="line">    fi</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<p>（3）在两台服务器上将nginx和keepalived启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">启动keepalived</span><br><span class="line">systemctl start keepalived.service</span><br></pre></td></tr></table></figure>

<p>nginx原理</p>
<blockquote>
<p>一个master和多个worker的好处</p>
</blockquote>
<ol>
<li>可以使用nginx -s reload 热部署</li>
<li>每个worker都是独立的</li>
</ol>
<blockquote>
<p>最终测试：在浏览器地址栏中输入虚拟网址192.168.227.50</p>
</blockquote>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202201152125138.png" alt="image-20220115212505670"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ip a</span><br><span class="line">查看到当前服务器已经绑定了这个虚拟ip</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202201152126961.png" alt="image-20220115212637331"></p>
<p>停止主服务器，再输入虚拟地址</p>
<blockquote>
<p>设置多少个worker合适</p>
</blockquote>
<p>worker数和cpu的核数相等最适宜</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">worker processes 8</span><br></pre></td></tr></table></figure>

<blockquote>
<p>连接数worker_connections</p>
</blockquote>
<p>发送请求，占有worker的几个连接数</p>
<p>2 / 4,访问的是静态资源就是两个连接数，访问动态资源就是四个连接数</p>
<p>nginx有一个master，四个worker，每个worker支持的最大连接数据1024，支持的最大并发数是多少？</p>
<ul>
<li>普通的静态访问最大并发数是：worker_connections * worker_processes / 2</li>
<li>而如果是Http作为反向代理来说，最大并发数应该是worker_connections * worker_processes/4</li>
</ul>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/12/04/Nginx%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/12/04/Zookeeper%E7%AC%94%E8%AE%B0/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Zookeeper
          
        </div>
      </a>
    
    
      <a href="/2021/12/04/MYSQL%E7%B4%A2%E5%BC%95/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">MySQL索引</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> Yuan
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="MyBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->
 
<script src="/js/clickLove.js"></script>
 
<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    <div id="music">
    
    
    
    <iframe frameborder="no" border="1" marginwidth="0" marginheight="0" width="200" height="52"
        src="//music.163.com/outchain/player?type=2&id=22707008&auto=1&height=32"></iframe>
</div>

<style>
    #music {
        position: fixed;
        right: 15px;
        bottom: 0;
        z-index: 998;
    }
</style>
    
    

  </div>
</body>

</html>